<?php
/**
 * SiteController.php
 *
 * @author: spiros kabasakalis <kabasakalis@gmail.com>
 * Date: 11/15/12
 * Time: 22:46 PM
 */

class SiteController extends Controller
{
	public function filters()
    {
        return array('accessControl');
    }
    /**
     * Declares class-based actions.
     */
    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }
	
	public function accessRules()
    {   	    	
				
        return array(
            array('allow',
                'actions'=>array('login'),                
            ),
             array('allow',
                'actions'=>array('logout'),                
            ),
            array('allow', // allow authenticated users to access all actions
                'users'=>array('@'),
            ),
            array('deny'),
        );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex()
    {	
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('index');
    }
    
     public function actionShop($id)
    {        
        
       
       //
       //$model = new LoginForm();
       //$model->SetLocation($id);
       
       //$log= new LoginForm;
       //$log->_identity= new UserIdentity;
       //
         $_SESSION['id']=$id;
         $user=$_SESSION['username'];
         $pass=$_SESSION['password'];
         $accno=$_SESSION['accno'];
         
         //$log = new UserIdentity($user, $pass, $accno);
         
         //$log->authenticate();
         $model= new LoginForm();
         $model->username=$user;
         $model->password=$pass;
         $model->accountNo=$accno;
         $model->login();
         
         
         $this->render('index',array('status'=>'active'));	
         //$this->render('index');
          //$this->redirect(Yii::app()->baseUrl);
          
         //Yii::app()->controller->refresh();
         //echo
       //$log= new LoginForm();
       //echo $user."--".$pass."--".$accno;
    }
    
    public function actionDemo()
    {        
        $this->render('demo');

    }
	
	public function actionProduct()
    {        
        $this->render('product');

    }
	
	public function actionProductDetail()
    {        
        $this->render('product_detail');

    }
	
	public function actionProductEdit()
    {        
        $this->render('product_edit');

    }
	
	public function actionProductImage()
    {        
        $this->renderPartial('product_image');

    }

	public function actionInvoice()
    {        
        $this->render('invoice');

    }

	public function actionInvoiceDetail()
    {        
        $this->render('invoice_detail');
    }
	
	public function actionOrder()
    {
        $this->render('order');

    }
	
	public function actionOrderDetail()
    {
        $this->render('order_detail');

    }

	public function actionCustomer()
    {
        $this->render('customer');
    }
	
	public function actionCustomerDetail()
    {
        $this->render('customer_detail');
    }
	
	public function actionCustomerEdit()
    {
        $this->render('customer_edit');
    }
	
	public function actionCustomerImage()
    {
        $this->renderPartial('customer_image');
    }
	
	public function actionSummary()
    {
        $this->render('summary');
    }
	
	


    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    

    public function actionLogin()
    {
       
        $model = new LoginForm();
        /*
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') 
        {
            echo CActiveForm::validate($model, array('username', 'password')); //, 'verify_code'
            Yii::app()->end();
        }

        if (isset($_POST['LoginForm'])) 
        {
            
            $model->rememberMe;
            $model->attributes = $_POST['LoginForm'];			
           // $model->accountNo = $_POST['LoginForm'];
            $_SESSION['username']=$model->username;
            $_SESSION['password']=$model->password;
           // $_SESSION['accno']=$model->accountNo;
           // $_SESSION['id']='';
            //Yii::app()->user->logout();
            
            if ($model->login()) {            	           
                        //Check user manager
                        if(UserInformation::getIsAdmin()!=1){
                            Yii::app()->user->logout();
                            $message="Bạn không có quyền đăng nhập  !";
                            $this->render('login', array('model' => $model,'message'=>$message));
                            return false;
                        }
                        $this->redirect(Yii::app()->baseUrl);				
				return;               
            }
        }
       */
        $this->render('login', array('model' => $model));


    }

    /**
     * This is the action that handles user's logout
     */
    
    public function actionLogout()
    {
        Yii::app()->user->logout();
        //$this->redirect('site/login');
        unset($_SESSION['locationName']);
          unset($_SESSION['username']);
            unset($_SESSION['password']);
            unset($_SESSION['accno']);
            session_destroy();
            //$_SESSION['id']='18';
        $this->redirect(Yii::app()->homeUrl);
        return;
        //$this->redirect('site/login');
    }
   


}